Podstawowe typy danych w Rubim


dr inż. Aleksander Smywiński-Pohl

apohllo@agh.edu.pl

http://apohllo.pl/dydaktyka/programowanie-obiektowe

konsultacje: wtorek 15:30 - 18:00, pokój 4.61

Typ logiczny w Rubim

In [1]:
if(nil)
  puts "Nil jest prawdą"
else
  puts "Nil jest fałszem"
end
  
if(false)
  puts "Fałsz jest prawdą"
else
  puts "Fałsz jest fałszem"
end
Nil jest fałszem
Fałsz jest fałszem
In [2]:
if(true)
  puts "Prawda jest prawdą"
end
  
if("false")
  puts "Łańcuch 'false' jest prawdą"
end
  
if(0)
  puts "Zero jest prawdą"
end
(pry):18: warning: string literal in condition
Prawda jest prawdą
Łańcuch 'false' jest prawdą
Zero jest prawdą
In [3]:
puts(true.to_s)
true

Wartości liczbowe

In [4]:
puts 100 ** 1000
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In [5]:
p 100 / 9
p 100 / 9.0
11
11.11111111111111
Out[5]:
11.11111111111111
In [7]:
10.times { print "x" }
xxxxxxxxxx
Out[7]:
10
In [8]:
p 10.to_s
"10"
Out[8]:
"10"
In [9]:
p "10".to_i
p "10".to_i(2)
10
2
Out[9]:
2
In [10]:
p 10 + 2 * 3
p 10.+(2.*(3))
16
16
Out[10]:
16

Tablice

In [11]:
a = [1,2,3]
p a[0]
p a[0..1]
p a[0...1]
p a[-2..-1]
1
[1, 2]
[1]
[2, 3]
Out[11]:
[2, 3]
In [12]:
b = ["a", nil, 3]
puts b
p(b)
["a", nil, 3]
["a", nil, 3]
Out[12]:
["a", nil, 3]
In [13]:
c = [3, 7, 0, -10, 11, 1.1]
p(c.sort)
[-10, 0, 1.1, 3, 7, 11]
Out[13]:
[-10, 0, 1.1, 3, 7, 11]
In [14]:
d = Array.new(10, 1)
p(d)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Out[14]:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Porównwanie wartości

In [15]:
x = 100
y = 100

if(x == y)
  puts "Wartości liczbowe są sobie równe"
else
  puts "Wartości liczbowe nie są sobie równe"
end
Wartości liczbowe są sobie równe
In [16]:
a = "Ala"
b = "Ala"
if(a == b)
  puts "Łańcuchy są sobie równe"
else
  puts "Łańcuchy nie są sobie równe"
end
Łańcuchy są sobie równe
In [17]:
a = [1,2,3]
b = [1,2,3]

if(a == b)
  puts "Tablice są sobie równe"
else
  puts "Tablice nie są sobie równe"
end
Tablice są sobie równe
In [18]:
c = [
        [1,2,3],
        [2,3,4]
    ]
d = [
        [1,2,3],
        [2,3,4]
    ]

if(c == d)
  puts "Tablice wielowymiarowe są sobie równe"
end
Tablice wielowymiarowe są sobie równe

Wartość pusta nil

In [19]:
puts nil.nil?
puts 1.nil?
true
false
In [20]:
a = nil

if(a.nil?)
  puts "Wartość pusta/niepoprawna"
else
  puts "Wartość niepusta"
end

p a.to_s
Wartość pusta/niepoprawna
""
Out[20]:
""

Pytania?